/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.hamacekj.geometr.internalplugins;

import cz.hamacekj.geometr.CircleObject;
import cz.hamacekj.geometr.Point;
import cz.hamacekj.geometr.plugin.ConstructionObject;
import cz.hamacekj.geometr.plugin.ConstructionPlugin;
import cz.hamacekj.geometr.plugin.IPointObject;
import cz.hamacekj.geometr.plugin.InputParameters;
import cz.hamacekj.geometr.plugin.InputParametersInterface;
import cz.hamacekj.geometr.plugin.ParameterItem;
import cz.hamacekj.geometr.plugin.ParameterObject;
import cz.hamacekj.geometr.plugin.ParameterString;
import cz.hamacekj.geometr.plugin.ParameterValue;
import cz.hamacekj.geometr.plugin.PrimitiveObject;
import cz.hamacekj.geometr.plugin.ValueArgument;
import java.util.HashSet;
import java.util.Set;
import javax.swing.ImageIcon;

/**
 * Plugin pro vytvoření kružnice.
 * @author hamacekh
 */
public class CirclePlugin implements ConstructionPlugin {

    private String error;
    
    @Override
    public String getError(){
        return error;
    }
    
    @Override
    public String getPluginName() {
        return "Kružnice";
    }

    @Override
    public InputParametersInterface[] getPossibleParameters() {
        InputParametersInterface[] res = new InputParametersInterface[1];
        res[0] = new SimpleParams();
        return res;
    }

    @Override
    public ImageIcon getIcon() {
        return null;
    }

    @Override
    public ConstructionObject[] createConstructionObject(InputParametersInterface parametersType, ConstructionObject[] objects, ValueArgument[] values) {
        if (parametersType instanceof SimpleParams){
            final String name = values[0].getRealInput();
            if(!Character.isLowerCase(name.charAt(0))){
                this.error = "Neplatný název kružnice";
                return null;
            }
            ConstructionObject c = objects[0];
            PrimitiveObject[] prim = c.getPrimitiveObjects().toArray(new PrimitiveObject[0]);
            if (!(prim[0] instanceof IPointObject)){
                return null;
            }
            final String centerName = c.getLabel();
            final String realInput = values[1].getRealInput();
            final IPointObject center = (IPointObject) prim[0];
            final double radius = values[1].getValue();
            if(Double.isNaN(radius) || radius <= 0){
                this.error = "Zadán neplatný poloměr";
                return null;
            }
            CircleObject circlePrimitive = new CircleObject(new Point(center.getPoint().getX(), center.getPoint().getY()), radius);
            final HashSet<PrimitiveObject> circleSet = new HashSet<>();
            circleSet.add(circlePrimitive);
            ConstructionObject circleObject = new ConstructionObject() {

                @Override
                public String getLabel() {
                    return name;
                }

                @Override
                public boolean isLabelShown() {
                    return true;
                }

                @Override
                public Set<PrimitiveObject> getPrimitiveObjects() {
                    return circleSet;
                }

                @Override
                public String getStepDescription() {
                    return this.getLabel()+": "+this.getLabel()+" = ("+centerName+", "+realInput+")";
                }
            };
            ConstructionObject[] res = { circleObject };
            return res;
        }
        return null;
    }
    
    
    private class SimpleParams extends InputParameters{

        @Override
        public String getParametersCaption() {
            return "Střed, poloměr";
        }

        @Override
        public ParameterItem[] getParameters() {
            ParameterItem[] res = new ParameterItem[6];
            res[0] = new ParameterValue() {

                @Override
                public String getCaption() {
                    return "Jméno";
                }
            };
            res[1] = new ParameterString() {

                @Override
                public String getText() {
                    return " = (";
                }
            };
            res[2] = new ParameterObject() {

                @Override
                public String getCaption() {
                    return "Střed";
                }

                @Override
                public String[] getObjectNames() {
                    String[] res = {"Bod"};
                    return res;
                }
            };
            res[3] = new ParameterString() {

                @Override
                public String getText() {
                    return ", ";
                }
            };
            res[4] = new ParameterValue() {

                @Override
                public String getCaption() {
                    return "Poloměr";
                }
            };
            res[5] = new ParameterString() {

                @Override
                public String getText() {
                    return ")";
                }
            };
            return res;
        }
        
    }
}
